function Z = pp_booth_encoded(B,T)
    % B: Two complement, will be Booth encoded using radix-4
    % T: Two complement
    B = booth_encoding(B);
    len_b = length(B);
    len_t = length(T);
    
    z = sum(B==0);
    Z = zeros(len_b-z,len_t+(len_b-1)*2);

    c = 1;
    for i = 1:len_b,
        len_l = (len_b-1)*2-(i-1)*2;
        len_r = (i-1)*2;
        switch B(len_b+1-i)
            case -2
                sign = not(T(1));
                Z(c,:) = [ones(1,len_l)*sign negate(T) zeros(1,len_r)];
                Z(c,:) = [Z(c,2:end) 0];
                c=c+1;                
            case -1
                sign = not(T(1));
                Z(c,:) = [ones(1,len_l)*sign negate(T) zeros(1,len_r)];
                c=c+1;
            case 0
                % skip
            case 1
                sign = T(1);
                Z(c,:) = [ones(1,len_l)*sign T zeros(1,len_r)];
                c=c+1;
            case 2
                sign = T(1);
                Z(c,:) = [ones(1,len_l)*sign T zeros(1,len_r)];
                Z(c,:) = [Z(c,2:end) 0];
                c=c+1;
            otherwise
                % error
        end
    end
end

function X = negate(X)
    X = not(X);
    X(end) = X(end)+1;
    X = carry_propagate(X);
end

function X = carry_propagate(X)
    C = 1;
    while(sum(C)>0)
        C = X==2;
        C_LS = [C(2:end) 0];
        X(X==2) = 0;
        X = X + C_LS;
    end
end